from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

import configparser
import os

class DisconfigException(BaseException):
    def __init__(self, *args: object) -> None:
        super().__init__(*args)

config = configparser.ConfigParser()

path = os.path.join(os.getcwd(), 'mini-judge', 'sql_config.ini')

config.read(path)

host = config['mysql']['host']
user = config['mysql']['user']
password = config['mysql']['password']

if host == '' or user == '' or password == '':
    raise DisconfigException('没有配置mysql')

MINIJUDGE_DATABASE_URL = f'mysql+mysqlconnector://{user}:{password}@{host}/minijudge?auth_plugin=mysql_native_password'
print(MINIJUDGE_DATABASE_URL)
engine = create_engine(MINIJUDGE_DATABASE_URL, encoding='utf8', echo=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()